<?php
/**
 * Description of OrderController
 *
 * @author eagle
 */

namespace Admin\Controller;

class OrderController extends AdminController{

    /**
     * 订单列表<淘宝返回值订单>
     */
    public function index(){
        $data = page('Order', '10', array(), array('alipay_trade_no' => 'desc'));
        $this->assign($data);
        $this->display();
    }
    
    /**
     * 申请肖像权记录列表
     */
    public function portraitList($username=''){
        $where = '';
        if(!empty($username)){
            $where = 'username like "%' .$username. '%"';
        }
        $pdata = page('Apply_portrait', 10, $where, array('md_apply_portrait.id' => 'desc'), 
                'md_user on md_apply_portrait.sid=md_user.id',
                'md_apply_portrait.id, total_money, begin_date, end_date, is_satisfy, comment, md_apply_portrait.status, sid, mid');
        $data = $pdata['list'];
//        dump($data);
        $exist_models = array();
        if(! empty($data)){
            foreach ($data as $key => $value){
                if(in_array($value['id'], $exist_models)){
                    unset($data[$key]);
                }else{
                    $exist_models[] = $value['id'];
                    $seller = M('User')->field('username')->where(array('id' => $value['sid']))->find();
                    $model = M('User')->field('username')->where(array('id' => $value['mid']))->find();
                    $data[$key]['seller'] = $seller['username'];
                    $data[$key]['model'] = $model['username'];
                }
            }
        }
        $pdata['list'] = $data;
        $this->assign($pdata);
        $this->display();
    }
    
    /**
     * 邀请体验记录列表
     */
    public function experienceList($username=''){                
        $where = '';
        if(!empty($username)){
            $where = 'username like "%' .$username. '%"';
        }
        $pdata = page('Invite_experience', 10, $where, array('md_invite_experience.id' => 'desc'), 
                'md_user on md_invite_experience.sid=md_user.id', 
                'md_invite_experience.id,mid,sid,title,total_fee,sub_time,return_type,experience,is_recommend,md_invite_experience.status');
        $data = $pdata['list'];
        if(! empty($data)){
            foreach ($data as $key => $value){
                $seller = M('User')->field('username')->where(array('id' => $value['sid']))->find();
                $model = M('User')->field('username')->where(array('id' => $value['mid']))->find();
                $data[$key]['seller'] = $seller['username'];
                $data[$key]['model'] = $model['username'];
            }
        }
        $pdata['list'] = $data;
        $this->assign($pdata);
        $this->display();
    }
    
    /**
     * 删除申请肖像权或者邀请体验记录
     * @param type $id   记录id
     * @param type $type 记录类型1-申请肖像权；2-邀请体验
     */
    public function delPortraitOrExperience($id, $type){
        if($type == 1){
            $model = M('Apply_portrait');
            $redirect = 'portraitList';
        }else{
            $model = M('Invite_experience');
            $redirect = 'experienceList';
        }
        $model->delete($id);
        M('Model_coin')->where(array('trade_id' => $id))->delete();
        $this->redirect('Admin/Order/'. $redirect);
    }

        /**
     * 平台虚拟货币列表<记录交易金额信息>
     */
    public function ModelcoinRecord($username=''){
        $where = 'md_model_coin.status=1 and uid !=0';
        if(!empty($username)){
            $where .= ' and username like "%' .$username. '%"';
        }
        $pdata = page('Model_coin', 10, $where, array('id' => 'desc'));
        $data = $pdata['list'];
        if(!empty($data)){
            foreach ($data as $key => $value){
                if($value['uid'] == 0){
                    $model = M('User')->where(array('id' => $value['uid']))->field('username')->find();
                    $data[$key]['username'] = $model['username'];
                }else{
                    $data[$key]['username'] = '平台';
                }
                $data[$key]['useful_icon'] = $value['after_trade_coin'] - $value['trust_coin'];//用户可提现金额
            }
        }
        $pdata['list'] = $data;
        $this->assign($pdata);
        $this->display();
    }
    
    /**
     * 刷新所有申请肖像权
     * 申请肖像权<肖像权到期的话，修改记录状态，同时将虚拟币从托管状态转为可提现>
     */
    public function updatePortraitState(){
        //查找条件<申请肖像权记录状态为5（托管），截止日期小于当前日期，虚拟货币表中记录类型为0（申请肖像权）>
        $where = array('md_apply_portrait.status' => 5, 'end_date' => array('lt', NOW_TIME));
        $record = M('Apply_portrait')->where($where)->select();
        if(!empty($record)){
            foreach ($record as $key => $value){
                $this->addModelCoin($value['id'], $value['total_money'], $value['mid']);
            }
        }
        M('Apply_portrait')->where($where)->save(array('status' => 6));
    }
    
    /**
     * 添加麻豆币记录<将托管麻币转为可提现麻币>
     * @param type $trade_id    申请肖像权记录id
     * @param type $total_fee   总交易额
     * @param type $uid         模特id
     */
    private function addModelCoin($trade_id, $total_fee, $uid){
        $income_percent = C('income_percent');
        $remark = '卖家申请肖像权';
        $model_income = $income_percent * $total_fee;   //模特收益
        $platform_income = $total_fee - $model_income;  //平台收益
        $model_coin = M('Model_coin');
        //模特麻豆币转换记录添加
        $last_model_record = $model_coin->where(array('uid' => $uid))->order(array('id' => 'desc'))->find();
        if(!empty($last_model_record)){
            $model_data = array(
                'uid'               => $uid,
                'user_type'         => 0,
                'trade_type'        => 0,
                'trade_id'          => $trade_id,
                'trust_coin'        => $last_model_record['trust_coin'] - $model_income,
                'current_trade_coin'=> $model_income,
                'after_trade_coin'  => $last_model_record['after_trade_coin'],
                'type'              => 2,
                'remark'            => $remark,
                'status'            => 1,
            );
        }
        $model_coin->add($model_data);  //添加模特麻豆币转换记录
        //
        //平台麻豆币转换记录添加
        $last_platform_record = $model_coin->where(array('uid' => 0))->order(array('id' => 'desc'))->find();
        if(! empty($last_platform_record)){
            $platform_data = array(
                'uid'               => 0,
                'user_type'         => 2,
                'trade_type'        => 0,
                'trade_id'          => $trade_id,
                'trust_coin'        => $last_platform_record['trust_coin'] - $platform_income,
                'current_trade_coin'=> $platform_income,
                'after_trade_coin'  => $last_platform_record['after_trade_coin'],
                'type'              => 2,
                'remark'            => $remark. ' 平台收益',
                'status'            => 1,
            );
        }
        $model_coin->add($platform_data);
    }
    
    /**
     * 检查模特推荐到试衣间的体验是否符合要求
     * @param type $id      体验id
     * @param type $status  状态：0-模特不推荐；1-模特推荐待审核；2审核通过；3审核未通过
     */
    public function checkFittingRoom($id, $status){
        if(M('Invite_experience')->save(array('id' => $id, 'is_recommend' => $status))){
            $this->ajaxReturn(array(
                'code' => 1,
                'msg'  => '审核成功'
            ));
        } else {
            $this->ajaxReturn(array(
                'code'  => 0,
                'msg'   => '审核失败',
            ));
        }
    }
    
    public function withdraw($username=''){
        $where = '';
        if(!empty($username)){
            $where = array('username'=>array('eq', $username));
        }
        $withdraw = page('Withdraw', 10, $where, array('manage_status' => 'asc'));
        if(!empty($data)){
            $this->assign($withdraw);
        }
        $this->display();
    }
}
